package com.dys.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @className: com.dys.servlet.ServletHttpSession02
 * @description: Servlet2 获得并取出HttpSession中的数据
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/20 22:09
 */
@WebServlet(urlPatterns = "/servlet2.do")
public class ServletHttpSession02 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取HttpSession
        HttpSession httpSession = req.getSession();
        // 从HttpSession中获取数据
        String username = (String) httpSession.getAttribute("username");
        System.out.println("username: " + username);
        String password = (String) httpSession.getAttribute("password");
        System.out.println("password: " + password);
        String level = (String) httpSession.getAttribute("level");
        System.out.println("level: " + level);

        // 获取Session对象的其他信息
        System.out.println("创建时间: " + new Date(httpSession.getCreationTime()).toLocaleString());
        System.out.println("最后一次访问时间: " + new Date(httpSession.getLastAccessedTime()).toLocaleString());
        System.out.println("最大不活动时间：: " + httpSession.getMaxInactiveInterval());
    }
}
